mui.init();
mui.plusReady(function(){
 	
	mui.getJSON("json/locat.json", function(data) {
	var sessionId=(plus.navigator.getCookie("sessionId")==null)?"":plus.navigator.getCookie("sessionId");
		var locat = data["url"];
			var factoryid=data["factoryid"];
			var factoryId=(plus.navigator.getCookie("factoryid")==null)?"":plus.navigator.getCookie("factoryid");
					if(factoryId != factoryid)
					window.location.href="login.html";
		//订单列表，如果没有登录。先进行登录
		mui.ajax(locat+'/clientOrder/order_list?sessionId='+sessionId, {
			dataType: 'json',
			type: 'get',
			timeout: 100000,
			success: function(data) {
				if("error" == data["msg"]) {
					window.location.href = "login.html";
				}else{
					if(data["message"]=="0"){
					mui.toast("亲还没有下货单呢，赶快下货吧！");
				}
				else{
					var finallist = '';
					var o_id = '';
					//				swal(locat+"/uploadFiles/twoDimensionCode/"+data["encoderImgId"]);
					for(i = data["jsonorderlists"].length - 1; i >= 0; i--) {

						var finallist1 = "<div data-am-tabs class='am-tabs'>" +
							"<ul class='am-tabs-nav am-nav am-nav-tabs'>" +
							"<li class='am-active'><a href='#tab1'>查看二维码</a></li>" +
							" <li class=''><a href='#tab2'>查看订单详情</a></li></ul>" +
							"<div class='am-tabs-bd'><div  class='am-tab-panel am-fade am-in am-active' id='tab1'>" +
							" <figure data-am-widget='figure' class='am am-figure am-figure-default' data-am-figure=\"{ pureview: '" + true + "' }\">";

						if(data["jsonorderlists"][i]["status"] == 0) {
							
//							swal("111111"+o_id);
							var status = 0;
							var finallist2 = "<img src='" + locat + "/uploadFiles/twoDimensionCode/" + data["encoderImgId"] + "' id='123" + data["jsonorderlists"][i]["ordernumber"] + "' /><figcaption class='am-figure-capition-btm'>" +
								" 提货单号： " + data["jsonorderlists"][i]["ordernumber"] + " </figcaption></figure></div>" +
								"<div  class='am-tab-panel am-fade' id='tab2'><div class='am-panel-bd' >" +
								" <div class='am-ckdd'>" +
								"<div class='am-g'><div class='am-u-sm-4 am-border-top'>订单编号:</div><div class='am-u-sm-8 am-border-top' >" + data["jsonorderlists"][i]["fac_order_no"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>商品名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["shopgoodsname"] + "</div></div>" +
								"<div class='am-g'><div class='am-u-sm-4'>吨数:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["data"] + "</div></div>	" +
								//"<div class='am-g'><div class='am-u-sm-4'>工厂名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["doc_factoryname"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>司机名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["shopdrivername"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>手机号码:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["driverphonenumber"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4 '>提货日期:</div><div class='am-u-sm-8 '>" + data["jsonorderlists"][i]["thdate"] + "</div></div>" +
								"<div class='am-g'><div class='am-u-sm-4'>操作</div><div class='am-u-sm-8'>" +
								"<button id='" + data['jsonorderlists'][i]['ordernumber'] + "' type='button' class='am-btn am-qx am-btn-primary am-radius ' onclick='editOrder(\"" + data['jsonorderlists'][i]['ordernumber'] + "\",\"" + data['jsonorderlists'][i]['o_id'] + "\")'  >取消订单</button></div></div></div></div></div></div></div>";

						} else if(data["jsonorderlists"][i]["status"] == 2){
							
							var status = 2;

							var finallist2 = "<img src='img/error.jpg' /><figcaption class='am-figure-capition-btm'>" +
								" 提货单号： " + data["jsonorderlists"][i]["ordernumber"] + " </figcaption></figure></div>" +
								"<div  class='am-tab-panel am-fade' id='tab2'><div class='am-panel-bd' >" +
								" <div class='am-ckdd'>" +
								"<div class='am-g'><div class='am-u-sm-4 am-border-top'>订单编号:</div><div class='am-u-sm-8 am-border-top'>" + data["jsonorderlists"][i]["fac_order_no"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>商品名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["shopgoodsname"] + "</div></div>" +
								"<div class='am-g'><div class='am-u-sm-4'>吨数:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["data"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>司机名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["shopdrivername"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>手机号码:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["driverphonenumber"] + "</div></div>	" +
								//"<div class='am-g'><div class='am-u-sm-4'>工厂名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["doc_factoryname"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4 '>提货日期:</div><div class='am-u-sm-8 '>" + data["jsonorderlists"][i]["thdate"] + "</div></div>" +
								"<div class='am-g'><div class='am-u-sm-4'>状态:</div><div class='am-u-sm-8'>" +
								"订单已取消</div></div></div></div></div></div></div>";

						}else{
							
							var status = 1;

							var finallist2 = "<img src='img/success.jpg' /><figcaption class='am-figure-capition-btm'>" +
								" 提货单号： " + data["jsonorderlists"][i]["ordernumber"] + " </figcaption></figure></div>" +
								"<div  class='am-tab-panel am-fade' id='tab2'><div class='am-panel-bd' >" +
								" <div class='am-ckdd'>" +
								"<div class='am-g'><div class='am-u-sm-4 am-border-top'>订单编号:</div><div class='am-u-sm-8 am-border-top'>" + data["jsonorderlists"][i]["fac_order_no"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>商品名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["shopgoodsname"] + "</div></div>" +
								"<div class='am-g'><div class='am-u-sm-4'>吨数:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["data"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>司机名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["shopdrivername"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>手机号码:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["driverphonenumber"] + "</div></div>	" +
								//"<div class='am-g'><div class='am-u-sm-4'>工厂名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["doc_factoryname"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4 '>提货日期:</div><div class='am-u-sm-8 '>" + data["jsonorderlists"][i]["thdate"] + "</div></div>" +
								"<div class='am-g'><div class='am-u-sm-4'>状态:</div><div class='am-u-sm-8'>" +
								"订单已出厂</div></div></div></div></div></div></div>";
						}
						finallist = finallist1 + finallist2;
						var pagecount = data["jsonorderlists"][i]["pageCount"];
					}
					//				swal(finallist);
					$("#orderlist").append(finallist);
					document.getElementById('pagecount').value = pagecount;
//					swal("2222222"+document.getElementById('editOrder').value );
					document.getElementById('editOrder').value = o_id;
//					swal("3333333"+document.getElementById('editOrder').value );
					document.getElementById('status').value = status;

				}
				}

			},
			error: function() {
					plus.ui.toast("","服务异常","error");
				    window.location.href=login.html;
				
			}
		});

	});

})



function see_more() {
	var currentPage = Number($("#currentPage").val()) + 1;
	//	swal(currentPage);

	var pagecount = Number($("#pagecount").val());
	mui.getJSON("json/locat.json", function(data) {
		var locat = data["url"];
		var sessionId=(plus.navigator.getCookie("sessionId")==null)?"":plus.navigator.getCookie("sessionId");
			var factoryid=data["factoryid"];
			var factoryId=(plus.navigator.getCookie("factoryid")==null)?"":plus.navigator.getCookie("factoryid");
					if(factoryId != factoryid)
					window.location.href="login.html";
		if(currentPage > pagecount) {
			mui.toast("亲！没有更多了！！！");
		} else {
			$("#loading").button('loading');
			mui.ajax(locat + '/clientOrder/order_list?sessionId='+sessionId, {
				dataType: 'json',
				data: {
					"currentPage": currentPage
				},
				type: 'get',
				timeout: 10000,
				success: function(data) {
					var finallist = '';
					var o_id = "";
					for(i = data["jsonorderlists"].length - 1; i >= 0; i--) {

						var finallist1 = "<div data-am-tabs class='am-tabs'>" +
							"<ul class='am-tabs-nav am-nav am-nav-tabs'>" +
							"<li class='am-active'><a href='#tab1'>查看二维码</a></li>" +
							" <li class=''><a href='#tab2'>查看订单详情</a></li></ul>" +
							"<div class='am-tabs-bd'><div  class='am-tab-panel am-fade am-in am-active' id='tab1'>" +
							" <figure data-am-widget='figure' class='am am-figure am-figure-default' data-am-figure=\"{ pureview: '" + true + "' }\">";

						if(data["jsonorderlists"][i]["status"] == 0) {
							

							var status = 0;
							var finallist2 = "<img src='" + locat + "/uploadFiles/twoDimensionCode/" + data["encoderImgId"] + "' id='123" + data["jsonorderlists"][i]["ordernumber"] + "'/><figcaption class='am-figure-capition-btm'>" +
								" 提货单号： " + data["jsonorderlists"][i]["ordernumber"] + " </figcaption></figure></div>" +
								"<div  class='am-tab-panel am-fade' id='tab2'><div class='am-panel-bd' >" +
								" <div class='am-ckdd'>" +
								"<div class='am-g'><div class='am-u-sm-4 am-border-top'>订单编号:</div><div class='am-u-sm-8 am-border-top'>" + data["jsonorderlists"][i]["fac_order_no"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>商品名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["shopgoodsname"] + "</div></div>" +
								"<div class='am-g'><div class='am-u-sm-4'>吨数:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["data"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>司机名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["shopdrivername"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>手机号码:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["driverphonenumber"] + "</div></div>	" +
								//"<div class='am-g'><div class='am-u-sm-4'>工厂名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["doc_factoryname"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4 '>提货日期:</div><div class='am-u-sm-8 '>" + data["jsonorderlists"][i]["thdate"] + "</div></div>" +
								"<div class='am-g'><div class='am-u-sm-4'>操作</div><div class='am-u-sm-8'>" +
								"<button id='" + data['jsonorderlists'][i]['ordernumber'] + "' type='button' class='am-btn am-qx am-btn-primary am-radius ' onclick='editOrder(\"" + data['jsonorderlists'][i]['ordernumber'] + "\",\"" + data['jsonorderlists'][i]['o_id'] + "\")'  >取消订单</button></div></div></div></div></div></div></div>";

						} else if(data["jsonorderlists"][i]["status"] == 2){
							
							var status = 2;

							var finallist2 = "<img src='img/error.jpg' /><figcaption class='am-figure-capition-btm'>" +
								" 提货单号： " + data["jsonorderlists"][i]["ordernumber"] + " </figcaption></figure></div>" +
								"<div  class='am-tab-panel am-fade' id='tab2'><div class='am-panel-bd' >" +
								" <div class='am-ckdd'>" +
								"<div class='am-g'><div class='am-u-sm-4 am-border-top'>订单编号:</div><div class='am-u-sm-8 am-border-top'>" + data["jsonorderlists"][i]["fac_order_no"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>商品名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["shopgoodsname"] + "</div></div>" +
								"<div class='am-g'><div class='am-u-sm-4'>吨数:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["data"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>司机名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["shopdrivername"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>手机号码:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["driverphonenumber"] + "</div></div>	" +
								//"<div class='am-g'><div class='am-u-sm-4'>工厂名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["doc_factoryname"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4 '>提货日期:</div><div class='am-u-sm-8 '>" + data["jsonorderlists"][i]["thdate"] + "</div></div>" +
								"<div class='am-g'><div class='am-u-sm-4'>状态:</div><div class='am-u-sm-8'>" +
								"订单已取消</div></div></div></div></div></div></div>";

						}else{
							
							var status = 1;

							var finallist2 = "<img src='img/success.jpg' /><figcaption class='am-figure-capition-btm'>" +
								" 提货单号： " + data["jsonorderlists"][i]["ordernumber"] + " </figcaption></figure></div>" +
								"<div  class='am-tab-panel am-fade' id='tab2'><div class='am-panel-bd' >" +
								" <div class='am-ckdd'>" +
								"<div class='am-g'><div class='am-u-sm-4 am-border-top'>订单编号:</div><div class='am-u-sm-8 am-border-top'>" + data["jsonorderlists"][i]["fac_order_no"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>商品名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["shopgoodsname"] + "</div></div>" +
								"<div class='am-g'><div class='am-u-sm-4'>吨数:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["data"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>司机名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["shopdrivername"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4'>手机号码:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["driverphonenumber"] + "</div></div>	" +
								//"<div class='am-g'><div class='am-u-sm-4'>工厂名称:</div><div class='am-u-sm-8'>" + data["jsonorderlists"][i]["doc_factoryname"] + "</div></div>	" +
								"<div class='am-g'><div class='am-u-sm-4 '>提货日期:</div><div class='am-u-sm-8 '>" + data["jsonorderlists"][i]["thdate"] + "</div></div>" +
								"<div class='am-g'><div class='am-u-sm-4'>状态:</div><div class='am-u-sm-8'>" +
								"订单已出厂</div></div></div></div></div></div></div>";
						}
						finallist = finallist1 + finallist2;

					}

					$("#orderlist").append(finallist);
					document.getElementById('currentPage').value = data["currentPage"];
					document.getElementById('editOrder').value = o_id;
					document.getElementById('status').value = status;
						$("#loading").button('reset');
					//						swal(document.getElementById('currentPage').value);
				},
				error: function() {
					//swal("error");
						$("#loading").button('reset');
					plus.ui.toast("","服务异常","error");
				    window.location.href=login.html;
				}
			});
		}

	});
}

function editOrder(id,o_id) {
	//var o_id = $("#editOrder").val();
	var status = Number($("#status").val());
	//		swal(id);
	//		swal(status);
//			swal(o_id);

		swal({
		title: "您确定要取消吗？",
		text: "您确定要取消这个订单吗？",
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: false,
		cancelButtonText: "取消",
		confirmButtonText: "确定",
		confirmButtonColor: "#ec6c62"
	}, function() {
		mui.getJSON("json/locat.json", function(data) {
		var locat = data["url"];
		mui.ajax(locat + '/clientOrder/order_cancel?o_id='+o_id, {
				type: 'GET',
				timeout: 10000,
				success: function(data, status, xhr) {
					$('#' + id).css({
						"background-color": "red",
						"border": "none"
					}).text("订单已取消");
					$('#123' + id).attr('src', 'img/error.jpg');
					$('#' + id).removeAttr('onclick');
					swal("订单已取消");
				},
				error: function(data) {
					//swal("error");
					plus.ui.toast("服务或网络异常，稍后再试！","","error");
				}
			});
	});
})
		
		
		
		
		

			

}